package me.shep.wishlist.client;

import java.util.HashMap;
import java.util.Map;

import me.shep.wishlist.client.RPC.InventoryServiceAsync;

import com.google.gwt.user.client.Cookies;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

public class ListItemsWidget extends Composite implements InventoryChangedCallback {
	Person person;
	Person user;
	Button add;
	DialogBox addDialog = new DialogBox();
	//DialogBox infoDialog;
	FlexTable table = new FlexTable();
	
	  /**
	   * A mapping of ineventory items to the widget display when the item is selected.
	   */
	  private Map<DesiredItem, DialogBox> itemToDialog = new HashMap<DesiredItem, DialogBox>();
	
	public ListItemsWidget(Person p, Person u) {
		person = p;
		user = u;
		refreshTable();
		initWidget(table);
	}
	
	public void deleteItemCallback(DesiredItem d) {
		person.getDesiredItems().remove(d);
		refreshTable();
		itemToDialog.get(d).hide();
		itemToDialog.remove(d);
		d = null;
	}
	public void purchasedItemCallback(DesiredItem d) {
		for(DesiredItem di : person.getDesiredItems()) {
			if (di.getId() == d.getId())
				di.setWhoPurchasedIt(d.getWhoPurchasedIt());
		}
		refreshTable();
	}
	
	public void addItemCallback(DesiredItem d) {
		person.getDesiredItems().add(d);
		refreshTable();
		//THERE IS A BIG BUG HERE -- WE NEED TO GET THE ID BACK IN ORDER TO DELETE, OR DELETE BASED ON SOMETHING ELSE
		//addDialog.hide();
	}
/*	
	AsyncCallback deleteItemCallback = new AsyncCallback() {
		public void onFailure(Throwable caught) {
			Window.alert("There was an error deleting the item!\n\n"+caught.getMessage());

		}

		public void onSuccess(Object arg0) {
			Window.alert("Item Deleted Succesfully");
			//delete.setText("Delete");
			//Return user to login page.  UNCOMMENT THIS ON DEPLOYMENT
			//Window.Location.assign(Window.Location.getHref().substring(0,Window.Location.getHref().lastIndexOf("/")+1));
		}
	};
	*/
	
	private void refreshTable() {
		
		table.clear();
		int i = 0;
		final InventoryChangedCallback icc = this;
		if (person.getDesiredItems().size() == 0)
			table.setText(i++, 0, user.getId() == person.getId() ? "You currently don't want anything." : person.getName() + " currently doesn't want anything.");
		for (DesiredItem d : person.getDesiredItems()) {
			final DesiredItem di = d;
			Label item = new Label((d.getName() == null || d.getName().equals("")) ? "Unnamed Item" : d.getName());
			item.setStyleName("wl-ItemLabel");
			//we don't need to check user here, as the server should have already wiped the purchased attribute
			if(d.getWhoPurchasedIt() != null)
				item.setStyleName("wl-PurchasedItemLabel");
			item.addClickListener(new ClickListener(){
				public void onClick(Widget sender) {
					final DialogBox infoDialog = new DialogBox();
					itemToDialog.put(di, infoDialog);
					infoDialog.setAnimationEnabled(true);
					infoDialog.setText(di.getName());

					VerticalPanel dialogContents = new VerticalPanel();
					dialogContents.setSpacing(4);
					infoDialog.setWidget(dialogContents);
/*
					Button delete = new Button("Delete", new ClickListener() {
						public void onClick(Widget sender) {
							InventoryServiceAsync.impl.deleteItem(di.getId(), Cookies.getCookie("token"), deleteItemCallback);
						}
					});*/

					Button closeButton = new Button("Close", new ClickListener() {
						public void onClick(Widget sender) {
							infoDialog.hide();
						}
					});
					/*
					HorizontalPanel buttonsHP = new HorizontalPanel();
					buttonsHP.add(delete);
					buttonsHP.add(closeButton);
					buttonsHP.setCellHorizontalAlignment(closeButton, HasHorizontalAlignment.ALIGN_RIGHT);*/
					dialogContents.add(new ItemInfoWidget(di, user, icc));
					dialogContents.add(closeButton);
					infoDialog.center();
					infoDialog.show();
					dialogContents.setCellHorizontalAlignment(closeButton,
							HasHorizontalAlignment.ALIGN_RIGHT);


				}      
			});
			table.setWidget(i, 0, item);
			/*if(user.getId() != person.getId())
				if(d.getWhoPurchasedIt() != null)
					table.setText(i, 3, "x");*/
			table.setWidget(i++, 1, new HTML("<a href='"+d.getUrl()+"' target='_blank_'>"+d.getUrl()+"</a>"));

		}
		//System.out.println(p.getDesiredItems().size());

		addDialog.setAnimationEnabled(true);
		addDialog.setText("Add a new item for " + person.getName()+":");
		VerticalPanel dialogContents = new VerticalPanel();
		dialogContents.setSpacing(4);
		addDialog.setWidget(dialogContents);

		Button closeButton = new Button("Close", new ClickListener() {
			public void onClick(Widget sender) {
				addDialog.hide();
			}
		});
		dialogContents.add(new AddItemWidget(person, user, this));
		dialogContents.add(closeButton);

		dialogContents.setCellHorizontalAlignment(closeButton,
				HasHorizontalAlignment.ALIGN_RIGHT);
		add = new Button("Add Item", new ClickListener() {
			public void onClick(Widget sender) {
				addDialog.center();
				addDialog.show();
			}
		});
		table.setWidget(i, 1, add);
	}
}
